package com.brave.service.service;

import com.brave.banner.repository.MCBannerInfoRepository;
import com.brave.base.BaseService;
import com.brave.service.dto.FindServiceListByTypeDTO;
import com.brave.service.entity.MCServiceDictTypeEntity;
import com.brave.service.repository.ServiceDictTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServiceQueryService extends BaseService
{
    @Autowired
    private ServiceDictTypeRepository serviceDictTypeRepository;
    @Autowired
    private MCBannerInfoRepository bannerInfoRepository;


    /**
     * 2019年10月10日 17:44:09
     * 查询出所有的服务类型
     * @return
     */
    public String findServiceTypeList()
    {
        //获取一级分类
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<MCServiceDictTypeEntity> firstLevelList = serviceDictTypeRepository.findByLevelOrderByTypeSortAsc(1);
        firstLevelList.stream().forEach(first ->
        {
            List<MCServiceDictTypeEntity> secondList = serviceDictTypeRepository.findByTypePidOrderByTypeSortAsc(first.getId());
            Map<String, Object> resultMap = new HashMap<>(10);
            resultMap.put("id", first.getId());
            resultMap.put("typeName", first.getTypeName());
            resultMap.put("typePid", first.getTypePid());
            resultMap.put("typeSort", first.getTypeSort());
            resultMap.put("level", first.getLevel());
            resultMap.put("childList", secondList);
            resultList.add(resultMap);
        });
        return success(resultList);
    }


    /**
     * 2019年10月10日 17:52:17
     * 查询对应的服务列表信息
     * @param dto
     * @return
     */
    public String findServiceListByType(FindServiceListByTypeDTO dto)
    {
        StringBuilder querySQL = new StringBuilder();
        querySQL.append("SELECT  " +
                " s.id serviceId, s.service_name serviceName,  " +
                " s.service_content serviceContent, " +
                " s.create_date createDate, u.id userId,  " +
                " u.user_name, u.head_url headUrl FROM mc_service_info s  " +
                " LEFT JOIN mc_user_base_info u ON s.create_user_id = u.id  " +
                "WHERE s.type_id = ").append(dto.getTypeId());
        Map<String, Object> resultMap = bannerInfoRepository.findPageList(querySQL.toString(), dto.getPageSize(), dto.getPageNumber());
        return success(resultMap);
    }



}